package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.MenuDto;
import com.project.hqshorttaxi.dto.MenuParam;
import com.project.hqshorttaxi.entity.Menu;
import com.project.hqshorttaxi.mapper.MenuMapper;
import com.project.hqshorttaxi.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public Menu getById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Menu entity) throws Exception {
        menuMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Menu entity) throws Exception {
        menuMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<Menu> listAll() {
        return menuMapper.selectAll();
    }

    @Override
    public List<Menu> listByPid(Integer pid) {
        Example example = new Example(Menu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("pid", pid);
        c.andEqualTo("deleteFlag", 0);
        List<Menu> list = menuMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<MenuDto> list(MenuParam param) {
        Example example = new Example(Menu.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getMenuName())) {
            c.andLike("menuName", "%" + param.getMenuName() + "%");
        }
        c.andEqualTo("deleteFlag", 0);
        example.orderBy("rank").asc();
        List<Menu> menus = menuMapper.selectByExample(example);
        List<Menu> mainMenus = menus.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());
        List<MenuDto> menuList = new ArrayList<MenuDto>();
        for (Menu main : mainMenus) {
            MenuDto dto = new MenuDto();
            dto.setId(main.getId());
            dto.setMenuId(main.getMenuId());
            dto.setMenuName(main.getMenuName());
            dto.setPid(main.getPid());
            dto.setPath(main.getPath());
            dto.setRank(main.getRank());
            dto.setCreateTime(main.getCreateTime());
            dto.setUpdateTime(main.getUpdateTime());
            dto.setDeleteFlag(main.getDeleteFlag());
            List<Menu> subMenus = menus.stream().filter(item -> item.getPid().equals(main.getMenuId())).collect(Collectors.toList());
            List<MenuDto> children = new ArrayList<MenuDto>();
            for (Menu sub : subMenus) {
                MenuDto subMenuDto = new MenuDto();
                subMenuDto.setId(sub.getId());
                subMenuDto.setPid(sub.getPid());
                subMenuDto.setPidMenuName(main.getMenuName());
                subMenuDto.setMenuId(sub.getMenuId());
                subMenuDto.setMenuName(sub.getMenuName());
                subMenuDto.setPath(sub.getPath());
                subMenuDto.setRank(sub.getRank());
                subMenuDto.setCreateTime(sub.getCreateTime());
                subMenuDto.setUpdateTime(sub.getUpdateTime());
                subMenuDto.setDeleteFlag(sub.getDeleteFlag());
                children.add(subMenuDto);
            }
            dto.setChildren(children);
            menuList.add(dto);
        }
        return menuList;
    }
}
